package servelet;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Alarm {
    public static void main(String[] args) {
 Timer timer = new Timer();
 timer.schedule(new task(timer), 1000, 1000);//不同之处
    }

    static class task extends TimerTask {
    	Timer timer;//多了个Timer属性，用它来cancel时间调度器
    	task(Timer timer){
    		this.timer = timer;
    	}
    	public void run() {
    		Date now = new Date();
    		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
    		if (time.format(now).toString().compareTo("09:44:00") == 0){
	    	 		timer.cancel();
	    	 		System.out.println("时间到");
    		}else{
    			System.out.println(time.format(now));
    		}
    	
    	}
    }
}